<?php

namespace App\Http\Controllers\ChatGroup\Web;

use App\Http\Controllers\BaseController;
use App\Models\ChatGroup\ChatGroupFAQModel;
use App\Models\ChatGroup\ChatGroupHistoryModel;
use App\Service\ChatGroup\ChatGroupHistoryService;
use App\Service\ChatGroup\ChatGroupSessionService;
use App\Service\ChatGroup\ChatGroupStructService;
use App\Service\Kms\Auth\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ChatMessageController extends BaseController
{
    public function send(Request $request): JsonResponse
    {
        $groupId     = $request->get('groupId');
        $type        = $request->get('type');
        $content     = $request->get('content');
        $clientMsgId = (string)$request->get('clientMsgId');

        $userId = getRmsUserId();
        $this->validate($request, [
            'groupId'     => 'required|integer|min:1',
            'clientMsgId' => 'required|string|min:1',
            'type'        => ['required', 'string', Rule::in(array_keys(ChatGroupHistoryModel::TYPES))],
            'content'     => 'required',
        ]);

        $isSendAi = ChatGroupHistoryModel::IS_SEND_AI_YES;

        $faqId = 0;
        if ($type == ChatGroupHistoryModel::TYPE_DIALOG) {
            $faqId    = $content['faqId'] ?? 0;
            $content  = ChatGroupFAQModel::geFaqtDialog($faqId);
            //$isSendAi = ChatGroupHistoryModel::IS_SEND_AI_NO;
            $faqId    = 0;
        }

        (new ChatGroupStructService($groupId, 0))
            ->sendMsgStruct($userId, $clientMsgId, $type, $content, $faqId, 0, $isSendAi)
            ->send();

        return $this->success();
    }

    public function lists(Request $request)
    {
        return $this->success();
    }

    public function recall(Request $request)
    {
        $userId = getRmsUserId();
        $msgId  = $request->get('msgId');

        $this->validate($request, [
            'msgId' => 'required|integer|min:1'
        ]);

        ChatGroupHistoryService::recall($msgId, $userId);

        return $this->success();
    }

    /**
     * 获得常见问题答案
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function sendFaqDialog(Request $request): JsonResponse
    {
        $this->validate($request, [
            'faqId'   => 'required|int',
            'groupId' => 'required|int',
        ]);
        $userId      = getRmsUserId();
        $faqId       = $request->post('faqId');
        $groupId     = $request->post('groupId');
        $clientMsgId = (string)$request->get('clientMsgId');

        // 发送消息
        (new ChatGroupStructService($groupId, 0))
            ->sendMsgStruct($userId, $clientMsgId, ChatGroupHistoryModel::TYPE_DIALOG, '', $faqId, 0, ChatGroupHistoryModel::IS_SEND_AI_YES)
            ->send();

        return $this->success();
    }
}
